<template>
  <div class="container">
    <header>
      <div class="title">商家列表</div>
    </header>

    <ul class="business-list">
      <li v-for="(business,index) in businesslist" :key="index" @click="toBusinessInfo(business.businessId)">
        <div class="left">
          <img :src="business.businessImg" alt />
          <div v-if="business.quantity>0">{{business.quantity}}</div>
        </div>
        <div class="business-info">
          <h3>{{business.businessName}}</h3>
          <p>&#165;{{business.starPrice}}起送 | &#165;{{business.deliveryPrice}}配送</p>
          <p>{{business.businessExplain}}</p>
        </div>
      </li>
    </ul>
    <Footer></Footer>
  </div>
</template>

<script>
import Footer from "../components/Footer";
export default {
  name: "BusinessList",
  components: {
    Footer
  },
  data() {
    return {
      orderTypeId: this.$route.query.orderTypeId,
      businesslist: [],
      cartlist: []
    };
  },
  created() {
    //判断用户是否登录，不登陆角标都为0，登录才显示
    let user = this.$getSessionStorage('user');
    //页面初始化请求后台，获取商家列表数据
    this.$axios
      .get(
        "/BusinessController/listBusinessByOrderTypeId?orderTypeId=" +
          this.orderTypeId
      )
      .then(response => {
        this.businesslist = response.data;
        for (let i = 0; i < this.businesslist.length; i++) {
          this.businesslist[i].quantity = 0;
        }
        if (user != null) {
          //根据用户id获取购物车数据
          this.getcartlist(user);
        }
      })
      .catch(error => {
        console.log(error);
      });
  },
  methods: {
    toBusinessInfo(businessId){
      this.$router.push({path:'/businessinfo',query:{'businessId':businessId}});
    },
    getcartlist(user) {
      this.$axios
        .get("/CartController/listCart?userId=" + user.userId)
        .then(response => {
          this.cartlist = response.data;
          for (let i = 0; i < this.businesslist.length; i++) {
            for (let j = 0; j < this.cartlist.length; j++) {
              if (
                this.businesslist[i].businessId ==
                this.cartlist[j].business.businessId
              ) {
                this.businesslist[i].quantity += this.cartlist[j].quantity;
              }
            }
          }
          this.businesslist.sort()
        })
        .catch(error => {
          console.log(error);
        });
    }
  }
};
</script>

<style scoped>
/* *********************** header 部分 ***************************** */
.container header {
  width: 100%;
  height: 12vw;
}
.container header .title {
  width: 100%;
  height: 12vw;
  background-color: #0097ff;
  text-align: center;
  line-height: 12vw;
  color: white;
  font-size: 4.8vw;
  position: fixed;
  top: 0;
  left: 0;
  z-index: 999;
}

/* *********************** 列表 部分 ***************************** */
.container .business-list li {
  box-sizing: border-box;
  padding: 2.5vw;
  display: flex;
  border-bottom: 1px solid #ddd;
  align-items: center;
}
.container .business-list li img {
  width: 20vw;
  height: 20vw;
}
.container .business-list li .left {
  position: relative;
}
.container .business-list li .left div {
  width: 5vw;
  height: 5vw;
  color: white;
  background-color: red;
  text-align: center;
  line-height: 5vw;
  border-radius: 50%;
  font-size: 3.6vw;
  position: absolute;
  right: -1.5vw;
  top: -1.5vw;
}
.container .business-list li .business-info {
  margin-left: 3vw;
}
.container .business-list li .business-info h3 {
  font-size: 3.8vw;
  color: #555;
}
.container .business-list li .business-info p {
  font-size: 3vw;
  color: #888;
  margin-top: 2vw;
}
</style>